//
//  JCNewFeatureViewController.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/12.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCNewFeatureViewController: UIViewController, UIScrollViewDelegate {

    var pageControl: UIPageControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        scrollView.delegate = self
        
        scrollView.backgroundColor = UIColor.whiteColor()
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        
        let count = 4
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imageView.size = scrollView.size
            imageView.x = ScreenWidth * CGFloat(i)
            
            scrollView.addSubview(imageView)
            //最后一张图片 设置btn
            if i == count-1 {
                setupLastPage(imageView)
            }
        }
        //设置内容大小
        scrollView.contentSize = CGSizeMake(ScreenWidth * CGFloat(count), 0)
        view.addSubview(scrollView)
        
        //添加pageControl
        let pageControl = UIPageControl()
        pageControl.centerX = ScreenWidth * 0.5
        pageControl.y = ScreenHeight * 0.9
        
        pageControl.numberOfPages = count
        pageControl.pageIndicatorTintColor = UIColor.blackColor()
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        view.addSubview(pageControl)
        self.pageControl = pageControl
    }
    
    private func setupLastPage(imageView: UIImageView){
        //开启用户交互
        imageView.userInteractionEnabled = true
        
        //进入按钮
        let enterButton = UIButton()
        //添加点击事件
        enterButton.addTarget(self, action: "enterBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        enterButton.setTitle("进入微博", forState: UIControlState.Normal)
        //先调整大小  再设置坐标
        enterButton.sizeToFit()
        enterButton.centerX = ScreenWidth * 0.5
        enterButton.y = imageView.height * 0.8
        imageView.addSubview(enterButton)
        
        let sharedButton = UIButton()
        //添加监听
        sharedButton.addTarget(self, action: "sharedBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        sharedButton.setTitle(" 分享到微博", forState: UIControlState.Normal)
        sharedButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        sharedButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        sharedButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        sharedButton.sizeToFit()
        sharedButton.centerX = enterButton.centerX
        sharedButton.y = imageView.height * 0.7
        imageView.addSubview(sharedButton)
        
    }
    //进入按钮点击事件
    @objc private func enterBtnDidClick(){
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        delegate.window?.rootViewController = delegate.defaultController()
    }
    
    //分享按钮点击事件
   @objc private func sharedBtnDidClick(sharedBtn: UIButton){
        sharedBtn.selected = !sharedBtn.selected
    }

    // MARK: -scrollerViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //计算滑动到第几页
        let page = scrollView.contentOffset.x / ScreenWidth
        self.pageControl?.currentPage = Int(page + 0.5)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
